package logic

import (
	"context"
	"srv/model/mysql"
	"srv/pkg"

	"srv/internal/svc"
	"srv/shenzhou"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserLogic {
	return &AddUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddUserLogic) AddUser(in *shenzhou.AddUserRequest) (*shenzhou.AddUserResponse, error) {
	password := pkg.GetMd5String(in.Password)
	u := mysql.User{
		Username: in.Username,
		Password: password,
		State:    int(in.State),
		Image:    in.Image,
	}
	err := u.AddUser()
	if err != nil {
		logx.Error("添加用户失败", err)
		return nil, err
	}

	return &shenzhou.AddUserResponse{}, nil
}
